package com.exampletest.service.impl;

import com.exampletest.mapper.EnterpriseMapper;
import com.exampletest.model.Enterprise;
import com.exampletest.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Override
    public boolean register(Enterprise enterprise) {
        if (enterpriseMapper.findByAccount(enterprise.getAccount()) != null) {
            return false;
        }
        enterprise.setCreateTime(new Date());
        enterprise.setStatus(1); // 默认激活状态
        return enterpriseMapper.insertEnterprise(enterprise) > 0;
    }

    @Override
    public Enterprise login(String account, String password) {
        Enterprise enterprise = enterpriseMapper.findByAccount(account);
        if (enterprise != null && enterprise.getPassword().equals(password)) {
            return enterprise;
        }
        return null;
    }

    @Override
    public List<Enterprise> getAllEnterprises() {
        return enterpriseMapper.findAll();
    }

    @Override
    public Enterprise getEnterpriseById(Integer id) {
        return enterpriseMapper.findById(id);
    }

    @Override
    public void updateEnterprise(Enterprise enterprise) {
        enterpriseMapper.updateEnterprise(enterprise);
    }

    @Override
    public void deleteEnterprise(Integer id) {
        enterpriseMapper.deleteById(id);
    }

    @Override
    public List<Enterprise> searchEnterprises(String keyword) {
        return enterpriseMapper.searchEnterprises(keyword);
    }

    @Override
    public boolean toggleStatus(Integer id, Integer status) {
        Enterprise enterprise = enterpriseMapper.findById(id);
        if (enterprise != null) {
            enterprise.setStatus(status);
            return enterpriseMapper.updateEnterprise(enterprise) > 0;
        }
        return false;
    }

    @Override
    public Enterprise verifyEnterprise(String account, String password) {
        return enterpriseMapper.findByAccountAndPassword(account, password);
    }
}